layui.define(['table', 'form','table'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,table = layui.table;

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/cdb/device/portable_battery/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'deviceSn', title: '设备序列号'}
      ,{field: 'cabinetDeviceSn', title: '机柜序列号'}
      ,{field: 'slotNum', title: '卡槽号'}
      ,{field: 'power', title: '电量'}
      ,{field: 'stateName', title: '状态'}
      ,{field: 'currentTradeNo', title: '当前订单'}
      ,{field: 'lastLeaseDate', title: '最后租借'}
      ,{field: 'lastHeartDate', title: '最后心跳'}
      ,{title: '操作', width: 180, align:'center', fixed: 'right', toolbar: '#LAY-table-toolbar'}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
    });
  });
  
  //事件
  var active = {
    info:function(obj){
    	layer.open({
            type: 2
            ,title: '共享充电宝机柜详情'
            ,content: layui.setter.webRoot + '/cdb/device/portable_battery/info.html?deviceSn='+obj.data.deviceSn
            ,maxmin: true
            ,area: ['90%', '80%']
        });
    }
    ,del:function(obj){
    	layer.confirm('确定要删除该共享充电宝机柜吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/cdb/device/portable_battery/del.do',
	          data:{deviceSn:obj.data.deviceSn},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
    }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('portable_battery_list', {})
});